﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using offline2048;
using MotionFramework.IO;
using UnityEngine;

namespace GTA
{
    public class DataMgr : Singleton<DataMgr>
    {   
        static readonly string EncryptKey = "card";
        static string fileName = "gta_data.json";
        public string lastSaveDataTime = "";
        public Dictionary<string, object> publicData = new Dictionary<string, object>();
        string publicDataStr = "";

        public bool inited = false;

        public void setValueWithKey(string key, object value) {
            if (publicData != null && publicData.ContainsKey(key)) {
                publicData[key] = value;
            }else {
                publicData.Add(key, value);
            }
        }

        public object getValueWithKey(string key, object defaultValue) {
            if (publicData != null && publicData.ContainsKey(key)) {
                return publicData[key];
            }else {
                return defaultValue;
            }
        }

        //广告相关存储数据
        public string ads_data_string = "";
        public string ADSDataString {
            set 
            {
                ads_data_string = value;
                SaveData();
            }
            get 
            {
                return ads_data_string;
            }
        }
        //公共存档数据
        public string public_data_string = "";
        public string PublicDataString {
            set 
            {
                public_data_string = value;
                SaveData();
            }
            get
            {
                return public_data_string;
            }
        }
        //游戏存档数据
        public string game_data_string = "";
        public string GameDataString {
            set 
            {
                game_data_string = value;
                SaveData();
            }
            get
            {
                return game_data_string;
            }
        }
        //本地通知相关数据
        public string notification_data_string = "";
        public string NotificationDataString {
            set 
            {
                notification_data_string = value;
                SaveData();
            }
            get
            {
                return notification_data_string;
            }
        }
        //iap 相关数据
        public string iap_data_string = "";
        public string IapDataString {
            set
            {
                iap_data_string = value;
                SaveData();
            }
            get
            {
                return iap_data_string;
            }
        }

        //Facebook 打点相关数据
        public string fb_event_string = "";
        public string FBEventDataString
        {
            set
            {
                fb_event_string = value;
                SaveData();
            }
            get
            {
                return fb_event_string;
            }
        }

        public void Init()
        {

            ParseData();
            AdsDataMgr.Instance.ParseData(ads_data_string);
            //PublicDataMgr.Instance.ParseData(public_data_string);
            //GameDataMgr.Instance.ParseData(game_data_string);
            //LoginDataMgr.Instance.ParseData(notification_data_string);
            // IapDataMgr.Instance.ParseData(iap_data_string);
            //FBEventDataMgr.Instance.ParseData(fb_event_string);

            inited = true;

        }

        public void SaveData()
        {
            lastSaveDataTime            = Utils.GetTimeStamp().ToString();
            ads_data_string             = AdsDataMgr.Instance.ParseToString();
            //public_data_string          = PublicDataMgr.Instance.ParseToString();
            //game_data_string            = GameDataMgr.Instance.ParseToString();
            //notification_data_string    = LoginDataMgr.Instance.ParseToString();
            // iap_data_string             = IapDataMgr.Instance.ParseToString();
            //fb_event_string             = FBEventDataMgr.Instance.ParseToString();

            publicDataStr               = Utils.ParseToString(publicData);
            Write();
        }

        /// <summary>
        /// 检测是否跨天，更新数据
        /// </summary>
        public void CheckUpdateDayData()
        {
            AdsDataMgr.Instance.CheckUpdateDayData();
            //LoginDataMgr.Instance.CheckUpdateDayData();
            //GameDataMgr.Instance.CheckUpdateDayData();
        }

        private void ParseData()
        {
            string data;
            try
            {
                data = Utils.ReadFile(fileName);
                GameLogger.Log("DataMgr parse:" + data);
                JsonUtility.FromJsonOverwrite(data, this);
                publicData = Utils.ParseToDictionary(publicDataStr);
               
            }
            catch
            {
                SaveData();
            }
        }

        private bool Write()
        {
            string data = JsonUtility.ToJson(this);
            try
            {
                Utils.SaveFile(data, fileName);
            }
            catch
            {
                return false;
            }
            return true;
        }
    }

}
